import model
from google.appengine.ext import db
from google.appengine.ext import webapp
import utility
import logging

class UserLogging(webapp.RequestHandler):
    def get(self):
        logs = db.GqlQuery("SELECT * FROM UserLog ORDER BY lastvisit DESC")
        page = '''<html><head></head><body style="background-color:#eee">'''
        page += '''<table align="center"><tr style="background-color:#2ca">'''
        page += '''<td align="center">IP Address</td>''' 
        page += '''<td align="center">Visit Hits</td>''' 
        page += '''<td align="center">Last Visit</td></tr>'''
        
        color1 = '''<tr style="background-color:#5eb"><td width=150 align="center">'''
        color2 = '''<tr style="background-color:#aeb"><td width=150 align="center">'''
        color = color1
        for item in logs:
            page += color + item.useraddr
            page += '''</td><td width=80 align="center">''' + str(item.searchhit)
            utc_time = item.lastvisit 
            shanghai_time = utility.translate(utc_time)
            #logging.debug(shanghai_time)
            page += '''</td><td width=420 align="center">''' + shanghai_time.strftime("%d. %b %Y %I:%M%p %Z")
            page += '</td></tr>'
            color1 = color2
            color2 = color
            color = color1
        page += '</table></body></html>'
        self.response.out.write(page)


